﻿<html><body><?java 
String ssid = rq.getSSID();
	if (ssid == null) {
	    List<String> selectedParams = rq.getSelectedParams();
	    if (selectedParams.isEmpty()) {
		return "NOT VALID";
	    }
	    List<Chapter> chaptersToTest = new ArrayList<Chapter>();
	    for (String id : selectedParams) {
		chaptersToTest.add(dao.getChapter(id));
	    }
	    ssid = dao.startNewSession(chaptersToTest);
	}

	Session session = dao.getSessionById(ssid);
	String prevAnswerId = rq.getValueByName("answer");
	if (prevAnswerId != null) {
	    String qid = rq.getValueByName("qid");
	    Answer prevAnswer = dao.getAnswerById(prevAnswerId);
	    session.answer(prevAnswer, qid);
	}
	String formHeader;
	String formFooter;
	if (session.hasNext()) {
	    formHeader = "<form action=\"./test.j\">";
	    formFooter = "<input type=submit value=\"Следущий вопрос\">\r\n"
		    + "</form>";
	} else {
	    formHeader = "<form action=\"./results.j\">";
	    formFooter = "<input type=submit value=\"Завершить\">\r\n"
		    + "</form>";
	}

	StringBuilder sb = new StringBuilder();
	sb.append(formHeader);
	Question next = session.getNext();
	sb.append("<h3>Вопрос:<pre>" + next.getText() + "</pre><h3>");
	for (Answer answer : next.getAnswers()) {
	    sb.append("<input type=\"radio\" name=\"answer\" value=\""
		    + answer.getId() + "\"> " + answer.getAnswer() + "<br>");
	}
	sb.append("<input type=hidden name=qid value=\"" + next.getId() + "\">");
	sb.append("<input type=hidden name=ssid value=\"" + ssid + "\">"
		+ formFooter);
	return sb.toString();
?java>
	
	
<hr>
<a href="/">Index</a>
</body><html>